import {removeToken, setToken} from "~/config/util/auth";

export default {
  /*-----------------用户信息-----------------*/
  login({commit}, userInfo) {
    const userName = userInfo.userName
    const password = userInfo.password
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        commit('SET_NAME', userName)
        commit('SET_TOKEN', 'testToken')
        setToken(userName)
        resolve()
      }, 500);

      // login({ userName: userName.trim(), password: password }).then(response => {
      //   const { data } = response
      //   commit('SET_TOKEN', data.token)
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    }).then(function (data) {//状态为fulfilled时执行
      console.log(data);
      console.log('这是成功操作');
    }, function (reason) { //状态为rejected时执行
      console.log(reason);
      console.log('这是失败的操作');
    })
  },
  // 获取用户信息
  getInfo({ commit, state }) {
  },
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      },100)
    }).then(function (data) {//状态为fulfilled时执行
      console.log(data);
      console.log('这是成功操作');
    }, function (reason) { //状态为rejected时执行
      console.log(reason);
      console.log('这是失败的操作');
    })
  },
  resetToken({ commit }) {
    // return new Promise(resolve => {
    //   removeToken() // must remove  token  first
    //   commit('RESET_STATE')
    //   resolve()
    // })
  },

  /*-----------------设置-----------------*/
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  },

  /*-----------------app-----------------*/
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  }
}
